﻿using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Xml.Linq;
using DynamicChart.Annotation;
using DynamicChart.Model;
using DynamicChart.Repository;
using DynamicChart.Util;

namespace DynamicChart.Service;

public class LoadService
{
    private List<ActuatorModel> _actuatorModels = new List<ActuatorModel>();
    
    private readonly IActuatorModelRepository _repository;

    public LoadService(IActuatorModelRepository repository)
    {
        _repository = repository;
    }
    
    public List<ActuatorModel>? LoadActuatorModel()
    {
        return _repository.GetAll() as List<ActuatorModel>;
    }

    public Dictionary<string, object> GetSeriesConfig(int id)
    {
        string config = _repository.GetSeriesConfig(id);
        
        return config == null ? null : JsonSerializer.Deserialize<Dictionary<string, object>>(config)!;
    }
    
    public bool Add(ActuatorModel actuatorModel)
    {
        try
        {
            _repository.Add(actuatorModel);
            return true;
        }
        catch (Exception ex)
        {
            throw new Exception("Error while adding actuator", ex);
        }
    }

    public bool Update(ActuatorModel actuatorModel)
    {
        try
        {
            _repository.Update(actuatorModel);
            return true;
        }
        catch (Exception ex)
        {
            throw new Exception("Error while adding actuator", ex);
        }
    }
}